package spring.cloud.tasks.client;

import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

@Slf4j
class WorkerTimerTask extends TimerTask {

    Worker worker;
    int count = 0;

    public WorkerTimerTask(Worker worker) {
        this.worker = worker;
    }

    public void run() {
        try {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            if (this.worker.zooKeeperManager.zooKeeperIsConnected() == false) {
                if (count > 5) {
                    this.worker.reStart();
                } else {
                    count = count + 1;
                }
            } else {
                count = 0;
                this.worker.pongpong();
            }
        } catch (Throwable e) {
            worker.pongpongErrorMessage = e.getMessage();
            log.error(worker.pongpongErrorMessage, e);
        }
    }
}
